package com.zlm.tools.lambda;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author zlm
 * @date 2023/8/18
 *
 * java8内置的四大核心函数式接口
 *
 * Consumer<T>  消费型接口
 *  void accept(T t);
 * Supplier<T>  供给型接口
 *  T get();
 * Function<T,R> 函数型接口
 *  R apply(T t);
 * Predicate<T>  断言型接口
 *  boolean test(T t);
 */
public class TestLambda {

    public static void main(String[] args) {
        //happy(1000, m -> System.out.println("消费了" + m + "元"));
        List<Integer> numList = getNumList(5, () -> (int) (Math.random() * 100));
        System.out.println(numList);
        System.out.println(strHandler("   退出 \t", String::trim));
        System.out.println(filterNum(numList, n -> (n & 1) == 0));
    }

    public static void happy(double money, Consumer<Double> consumer){
        consumer.accept(money);
    }

    public static List<Integer> getNumList(int num, Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

    public static String strHandler(String str, Function<String,String> function){
        return function.apply(str);
    }

    public static List<Integer> filterNum(List<Integer> list, Predicate<Integer> predicate){
        List<Integer> newList = new ArrayList<>();
        for (Integer num : list) {
            if(predicate.test(num)){
                newList.add(num);
            }
        }
        return newList;
    }


}
